/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.architecture;

import org.openaecfm.model.architecture.DoorLiningProperties;
import org.openaecfm.model.kernel.Root;
import org.openaecfm.model.representation.ShapeAspect;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class DoorLiningPropertiesIO extends
		AbstractEntityInstanceInputImpl<DoorLiningProperties> implements
		IEntityInstanceOutput<DoorLiningProperties> {

	private IEntityInstanceUtilInput<Root> utilInput;
	private IEntityInstanceUtilOutput<Root> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Root> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(DoorLiningProperties pojo,
			IAppDataManager appDataManager) {

		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCDOORLININGPROPERTIES", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set LiningDepth
		if (pojo.getLiningDepth() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getLiningDepth()));
		}

		// Set LiningThickness
		if (pojo.getLiningThickness() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getLiningThickness()));
		}

		// Set ThresholdDepth
		if (pojo.getThresholdDepth() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getThresholdDepth()));
		}

		// Set ThresholdThickness
		if (pojo.getThresholdThickness() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getThresholdThickness()));
		}

		// Set TransomThickness
		if (pojo.getTransomThickness() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTransomThickness()));
		}

		// Set TransomOffset
		if (pojo.getTransomOffset() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTransomOffset()));
		}

		// Set LiningOffset
		if (pojo.getLiningOffset() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getLiningOffset()));
		}

		// Set ThresholdOffset
		if (pojo.getThresholdOffset() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getThresholdOffset()));
		}

		// Set CasingThickness
		if (pojo.getCasingThickness() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getCasingThickness()));
		}

		// Set CasingDepth
		if (pojo.getCasingDepth() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getCasingDepth()));
		}

		// Set ShapeAspectStyle
		if (pojo.getShapeAspectStyle() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getShapeAspectStyle())));
		}

		// Set LiningToPanelOffsetX
		if (pojo.getLiningToPanelOffsetX() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getLiningToPanelOffsetX()));
		}

		// Set LiningToPanelOffsetY
		if (pojo.getLiningToPanelOffsetY() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getLiningToPanelOffsetY()));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected DoorLiningProperties doCreateObject() {
		return new DoorLiningProperties();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, DoorLiningProperties pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ShapeAspectStyle
		Parameter sas = sei.getParameter(14);
		if (sas.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) sas;
			pojo.setShapeAspectStyle((ShapeAspect) appDataManager.getObject(n
					.toInstanceName()));
		}
	}

	public void setUtilInput(IEntityInstanceUtilInput<Root> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			DoorLiningProperties pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set LiningDepth
		Parameter ld = sei.getParameter(4);
		if (ld.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) ld;
			pojo.setLiningDepth(r.toDouble());
		}

		// Set LiningThickness
		Parameter lt = sei.getParameter(5);
		if (lt.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) lt;
			pojo.setLiningThickness(r.toDouble());
		}

		// Set ThresholdDepth
		Parameter td = sei.getParameter(6);
		if (td.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) td;
			pojo.setThresholdDepth(r.toDouble());
		}

		// Set ThresholdThickness
		Parameter tt = sei.getParameter(7);
		if (tt.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) tt;
			pojo.setThresholdThickness(r.toDouble());
		}

		// Set TransomThickness
		Parameter trt = sei.getParameter(8);
		if (trt.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) trt;
			pojo.setTransomThickness(r.toDouble());
		}

		// Set TransomOffset
		Parameter to = sei.getParameter(9);
		if (to.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) to;
			pojo.setTransomOffset(r.toDouble());
		}

		// Set LiningOffset
		Parameter lo = sei.getParameter(10);
		if (lo.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) lo;
			pojo.setLiningOffset(r.toDouble());
		}

		// Set ThresholdOffset
		Parameter tho = sei.getParameter(11);
		if (tho.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) tho;
			pojo.setThresholdOffset(r.toDouble());
		}

		// Set CasingThickness
		Parameter ct = sei.getParameter(12);
		if (ct.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) ct;
			pojo.setCasingThickness(r.toDouble());
		}

		// Set CasingDepth
		Parameter cd = sei.getParameter(13);
		if (cd.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) cd;
			pojo.setCasingDepth(r.toDouble());
		}

		// Set LiningToPanelOffsetX
		Parameter lx = sei.getParameter(14);
		if (lx.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) lx;
			pojo.setLiningToPanelOffsetX(r.toDouble());
		}

		// Set LiningToPanelOffsetY
		Parameter ly = sei.getParameter(15);
		if (ly.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) ly;
			pojo.setLiningToPanelOffsetY(r.toDouble());
		}
	}
}
